<?php if ( ! defined("BASEPATH")) exit("No direct script access allowed");
class relax_comics extends MX_Controller
{
    private $cTitle = '';
    public function __construct()
    {
        parent::__construct();
        $this->load->model("relax_articles_model");
        $this->relax_articles_model->cate = 'relax_comics';
        $this->relax_articles_model->cid = 5;
        $this->cTitle = $this->relax_articles_model->getCategoryName(5);
    }
  
    public function sort($field = "created", $order = "desc")
    {
        $this->relax_articles_model->dataSort($field, $order);
        $this->index();
    }

    public function index($target = 0)
    {
        $arr = $this->relax_articles_model->dataIndex($target); 
        $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » <span>'.$this->cTitle.'</span>';
        $data = array(
            "content" => $this->load->view("relax_articles/main", $arr ,true),
            "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    } 
  
    public function find()
    {
        if (!isset($_POST["txt_find"])) return redirect(site_url("relax_comics/index"));
        if (isset($_POST["btnReset"]) && $_POST["btnReset"] == "Reset") 
            $this->session->unset_userdata("sm_find_relax_articles");
        else {
            $keyword = $this->input->post("txt_find", true);
            if ($keyword != "") {
                notify_set_messages(notify_info(t("Result")." ".$keyword), "relax_articles");
                $this->session->set_userdata("sm_find_relax_articles", $keyword);
            }
        }
    
        redirect(site_url("relax_comics/index"));
    }
  public function filterrelax_categories()
  {
    $id = $_POST["cid"];
    if (isset($_POST["btnReset"])) 
        $this->session->unset_userdata("sm_filter_relax_categories");
    else 
        $this->session->set_userdata("sm_filter_relax_categories", $id);
    redirect(site_url("relax_comics"));
  }    
    public function add()
    {        
        $arr = $this->relax_articles_model->dataAdd();
        $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » 
        <a href="/admin/relax_comics">'.$this->cTitle.'</a> » <span>'.t("Add").'</span>';
        $data = array("content" => $this->load->view("relax_articles/add", $arr, true), 
                      "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }    
  
    public function edit($id)
    {       
        $arr = $this->relax_articles_model->dataEdit($id);
        $breakCrum = '<a href="'.base_url().'">'.t("Home").'</a> » 
        <a href="/admin/relax_comics">'.$this->cTitle.'</a> » <span>'.t("Edit").'</span>';
        $data = array("content" => $this->load->view("relax_articles/edit", $arr, true),
                "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }
  
    public function doAdd()
    {
        if(!$this->relax_articles_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "relax_articles");
            return $this->add();
        }
        if ($this->relax_articles_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is added")), "relax_articles");
            return redirect(site_url("relax_comics"));    
        }
      
    }
  
    public function doEdit()
    {        
        if(!$this->relax_articles_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "relax_articles");
            return $this->edit(decrypt($_POST["idx"]));        
        }
        if ($this->relax_articles_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is updated")), "relax_articles");
            redirect(site_url("relax_comics"));   
        }
     
    } 
  
    public function del($id)
    {
        $result = $this->relax_articles_model->del($id);
        if (is_array($result) && $result[0] == -1) {
            notify_set_messages(notify_atten($result[1]), "relax_articles");
        } else {
            notify_set_messages(notify_success(t("Record is deleted")), "relax_articles");
        }
        redirect(site_url("relax_comics"));
    }     
        
}